//
//  hobbyVC.m
//  HappyStore
//
//  Created by 李小辉 on 16/6/12.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LabelVC.h"
#import "myDetailVC.h"
@interface LabelVC ()<UITextFieldDelegate>{
    NSInteger count;
}
@property (strong, nonatomic) UIButton * addBtn;
@property (strong, nonatomic) UILabel * industry;
@property (strong, nonatomic) NSMutableArray * labelArr;
@property (strong, nonatomic) UITextField *tf;
@end

@implementation LabelVC

- (NSMutableArray *)hobbyArr{
    if (!_hobbyArr) {
        _hobbyArr = [NSMutableArray array];
    }
    return _hobbyArr;
}
-(NSMutableArray *)labelArr{
    if (!_labelArr) {
        _labelArr = [NSMutableArray array];
    }
    return _labelArr;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = NO;
    navBarHairlineImageView.alpha = 0.5f;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    //找出导航栏的下边的黑线
    UIImageView *navBarHairlineImageView = [NaviBarLine findHairlineImageViewUnder:self.navigationController.navigationBar];
    navBarHairlineImageView.hidden = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = self.titleString;
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    
    count = self.hobbyArr.count;
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
    rightItem.tintColor = RGB(45, 45, 45);
    self.navigationItem.rightBarButtonItem = rightItem;
    
    //添加标签
    [self showViewButton];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
//返回按钮
-(void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}



-(void)btnClick{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入标签名称,最多四个字符" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    self.tf = [alert textFieldAtIndex:0];
    self.tf.placeholder = @"请输入您的标签";
    
    self.tf.delegate = self;
    [self.tf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    [alert showAlertWithCompletionHandler:^(NSInteger buttonIndex) {
        if(buttonIndex == 1){//点击确定

            if (self.tf.text.length > 4) {

                [self showSingleIconBubble:@"超过字数限制"];
                
            }else{
                
                self.tf=[alert textFieldAtIndex:0];
                if (self.tf.text.length > 0) {
                    [self.hobbyArr addObject:self.tf.text];
                    //显示标签
                    [self showViewButton];
                }
                
            }
        }

    }];
}
#pragma mark - 显示标签
- (void)showViewButton{
    for (UIView *view in [self.view subviews]) {
        [view removeFromSuperview];
    }
    //最多输入五个标签
    UILabel *industry = [[UILabel alloc]initWithFrame:CGRectMake(20, 18 + 64, 130, 30)];
    industry.text = @"最多输入五个标签";
    industry.textColor = [UIColor blackColor];
    industry.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:industry];
    //创建标签
    NSMutableArray *btnAry = [NSMutableArray arrayWithArray:self.hobbyArr];
    if (btnAry.count <= 4) {
        [btnAry addObject:@"添加"];
    }else{

        [self showErrorWithTitle:@"标签已满" autoCloseTime:2];
    }
    for (int i = 0; i<btnAry.count; i++) {
        UIButton *labelBtn = [[UIButton alloc] init];
        [labelBtn setTitle:btnAry[i] forState:UIControlStateNormal];
        labelBtn.height = 25;
        labelBtn.width = 75;
        labelBtn.x = i%3*((screenW- 20)/3)+20;
        labelBtn.y = i/3*40 + 120;
        labelBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        labelBtn.layer.masksToBounds = YES;
        labelBtn.layer.borderColor = [UIColor colorWithRed:201/255.0f green:202/255.0f blue:202/255.0f alpha:1.0f].CGColor;
        [labelBtn setTitleColor:RGB(137, 137, 137) forState:UIControlStateNormal];
        labelBtn.layer.borderWidth = 1.0f;
        labelBtn.layer.cornerRadius = 5.0f;
        
        //创建删除按钮
        UIButton *deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(labelBtn.x - 3, labelBtn.y - 3, 15, 15)];
        [deleteBtn setImage:[UIImage imageNamed:@"删除label"] forState:UIControlStateNormal];
        [deleteBtn addTarget:self action:@selector(deleteBtn:) forControlEvents:UIControlEventTouchUpInside];
        //判断是“添加”按钮就可以点击
        if ([btnAry[i] isEqualToString:@"添加"]) {
            labelBtn.userInteractionEnabled = YES;
            [labelBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:labelBtn];
        }else{
            labelBtn.userInteractionEnabled = NO;
            deleteBtn.tag = i;
            [self.view addSubview:labelBtn];
            [self.view addSubview:deleteBtn];
        }
        
    }
}
//删除标签
- (void)deleteBtn:(UIButton *)btn{
    [self.hobbyArr removeObject:self.hobbyArr[btn.tag]];
    [self showViewButton];
}

//进行字数限制
- (void)textFieldDidChange:(UITextField *)textField
{

    NSString *toBeString = textField.text;
    NSString *lang = self.textInputMode.primaryLanguage;//键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) {//简体中文输入，包括简体拼音，简体手写
        UITextRange *selectedRange = [textField markedTextRange];//获取高亮部分
        //没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!selectedRange) {
            if (toBeString.length > 4) {
                textField.text = [toBeString substringToIndex:4];
            }
        }else{//有高亮选择的字符串，则暂不对文字进行统计和限制
            
        }
    }else{//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > 4) {
            textField.text = [toBeString substringToIndex:4];
        }
    }
    
}
#pragma mark - 保存
-(void)rightBarButtonItemClick{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:self.hobbyArr forKey:@"biaoqian"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"biaoqian" object:nil userInfo:dic];
    [self.navigationController popViewControllerAnimated:YES];
}


@end
